package com.hanbang.taitaigou.ui.activity.application;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.home.MainActivity;
import com.hanbang.taitaigou.utils.other.ObjectUtils;

import java.util.ArrayList;
import java.util.List;


public class LeadActivity extends Activity {

    private ViewPager viewPager = null;
    private List<View> list = new ArrayList<View>();
    private MyAdapter myAdapter = null;
    private RadioGroup dotLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lead);
        initView();
    }

    @SuppressLint("InflateParams")
    private void initView() {
        dotLayout = (RadioGroup) findViewById(R.id.radioGroup);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        RadioGroup.LayoutParams params2 = new RadioGroup.LayoutParams(
                ObjectUtils.dip2px(this, 8), ObjectUtils.dip2px(this, 8));
        params2.setMargins(ObjectUtils.dip2px(this, 8), 0,
                ObjectUtils.dip2px(this, 8), 0);
        int image[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
        int imageBack[] = {0xffffffff, 0xffB1A29D, 0xffDD192F};
        for (int i = 0; i < image.length; i++) {
            View view = getLayoutInflater().inflate(R.layout.lead_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
            ll.setBackgroundColor(imageBack[i]);
            imageView.setImageResource(image[i]);
            list.add(view);
            if (i == image.length - 1) {
                list.get(i).setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(LeadActivity.this, MainActivity.class);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        startActivity(intent);
                        finish();
                    }
                });
            }
            // 设置圆点
            RadioButton r = new RadioButton(this);
            r.setLayoutParams(params2);
            r.setBackgroundResource(R.drawable.dot_focus);
            r.setButtonDrawable(R.drawable.dot_focus);
            r.setClickable(false);
            dotLayout.addView(r);
        }

        myAdapter = new MyAdapter();
        viewPager.setAdapter(myAdapter);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                ((RadioButton) dotLayout.getChildAt(arg0)).setChecked(true);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        ((RadioButton) dotLayout.getChildAt(0)).setChecked(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position), 0);// 添加页卡
            return list.get(position);
        }
    }

    @Override
    public void onBackPressed() {
    }
}
